import 'package:client_ui/api/Request.dart';
import 'package:client_ui/api/api/ConnectApi.dart';
import 'package:flutter/material.dart';

import '../component/UserComponent.dart';
import '../util/GlobalData.dart';
import 'UserChatPage.dart';

class IndexPage extends StatefulWidget {
  const IndexPage({super.key});

  @override
  State<IndexPage> createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  get onPressed => null;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Row(
      children: [generateUserList(), const Expanded(child: UserChatPage())],
    ));
  }

  @override
  void initState() {
    super.initState();
    ConnectApi.connect(1);
    Request.getInstance()?.listenToMessages(context);
  }

  @override
  void dispose() {
    super.dispose();
    Request.getInstance()?.closeConnection();
  }

  Widget generateUserList() {
    return Column(children: [
      Row(
        children: [
          IconButton(
            icon: Icon(Icons.zoom_in_map),
            onPressed: () {
              // 处理搜索按钮的点击事件
              print('Search icon clicked');
            },
          ),
          IconButton(
            icon: Icon(Icons.add),
            onPressed: () {
              // 处理更多按钮的点击事件
              print('More icon clicked');
            },
          ),
        ],
      ),
      Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: generateUserComponentList(),
      )
    ]);
  }

  List<Widget> generateUserComponentList() {
    return <Widget>[
      const UserComponent(
        title: 'User 1',
        avatarUrl:
            'https://picx.zhimg.com/v2-c87d34b747c2d81f7cd09fd87dfee2a1_1440w.jpg',
      ),
      const UserComponent(
        title: 'User 2',
        avatarUrl:
            'https://picx.zhimg.com/v2-6bf127b1206f928514b62c8a114d4405_1440w.jpg',
      ),
    ];
  }
}
